﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Data;

namespace QuanLyKho.DKChung
{
    public partial class DKChung_DKCuaHang : Form
    {
        public static string MaID = "0";
        public bool update = false;

        public DKChung_DKCuaHang()
        {
            InitializeComponent();
            LoadData();
        }

        public void LoadData()
        {
            if (MaID != "0")
            {
                var data = DataAll.SelectAll("DkChung_CuaHang", "*", " WHERE ID=" + MaID);
                txtTenCuaHang.Text = data.Rows[0]["TenCuaHang"].ToString();
                txtSoDienThoai.Text = data.Rows[0]["SoDienThoai"].ToString();
                txtDiaChi.Text = data.Rows[0]["DiaChi"].ToString();
            }
        }

        public void ResetControl()
        {
            txtMaCuaHang.Text = string.Empty;
            txtTenCuaHang.Text = string.Empty;
            txtSoDienThoai.Text = string.Empty;
            txtDiaChi.Text = string.Empty;
        }

        private void bntLuuLai_Click(object sender, EventArgs e)
        {
            if (update)
            {
                string message = DataAll.Update("DkChung_CuaHang", "MaCuaHang=N'" + txtMaCuaHang.Text + "',TenCuaHang=N'" + txtTenCuaHang.Text + "', DiaChi=N'" + txtDiaChi.Text
                    + "',SoDienThoai=N'" + txtSoDienThoai.Text + "' WHERE ID=" + MaID);
                MessageBox.Show(message.Equals("success") ? "Sửa cửa hàng thành công" : message, "Message",
                                MessageBoxButtons.OK,
                                message.Equals("success") ? MessageBoxIcon.Information : MessageBoxIcon.Error);
                Close();
            }
            else
            {
                string message = DataAll.InsertAll("DkChung_CuaHang", txtMaCuaHang.Text + "," + txtTenCuaHang.Text + "," + txtDiaChi.Text + "," + txtSoDienThoai.Text);
                MessageBox.Show(message.Equals("success") ? "Thêm cửa hàng thành công" : message, "Message",
                                MessageBoxButtons.OK,
                                message.Equals("success") ? MessageBoxIcon.Information : MessageBoxIcon.Error);
                if (message.Equals("success"))
                {
                    ResetControl();
                }
            }
        }

        private void bntThoat_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
